package com.jfs.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jfs.cmd.SIPCommander;
import com.jfs.constant.SipConstant;
import com.jfs.entity.Device;
import com.jfs.entity.DeviceChannel;
import com.jfs.entity.dto.DeviceDropListDto;
import com.jfs.entity.vo.DeviceUpdateByNameVo;
import com.jfs.mapper.DeviceMapper;
import com.jfs.result.Result;
import com.jfs.service.DeviceChannelService;
import com.jfs.service.DeviceService;
import com.jfs.zlk.config.ZlkMediaServerInfo;
import com.jfs.zlk.entity.StreamInfo;
import com.jfs.zlk.service.ZlkMediaServerService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description 设备接口实现层
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/7/22
 */
@Service
@Slf4j
@AllArgsConstructor
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {

    private final DeviceChannelService deviceChannelService;
    private final SIPCommander sipCommander;
    private final ZlkMediaServerInfo zlkMediaServerInfo;
    private final ZlkMediaServerService zlkMediaServerService;

    @Override
    public List<DeviceDropListDto> dropList() {
        List<DeviceDropListDto> dtoList = new ArrayList<>();
        List<Device> deviceList = this.list(Wrappers.<Device>lambdaQuery()
                .select(Device::getDeviceId, Device::getName));
        if (CollectionUtil.isNotEmpty(deviceList)) {
            for (Device device : deviceList
            ) {
                DeviceDropListDto deviceDropListDto = new DeviceDropListDto();
                BeanUtils.copyProperties(device, deviceDropListDto);
                dtoList.add(deviceDropListDto);
            }
        }
        return dtoList;
    }

    @Override
    @Transactional
    public Result<Object> updateByName(DeviceUpdateByNameVo deviceUpdateByNameVo) {
        boolean update = this.update(Wrappers.<Device>lambdaUpdate()
                .set(Device::getName, deviceUpdateByNameVo.getName())
                .eq(Device::getDeviceId, deviceUpdateByNameVo.getDeviceId())
        );
        if (update) {
            return Result.success("修改设备名称成功", null);
        }
        return Result.failed("修改设备名称失败");
    }

    @Override
    public Result<Object> playStream(String channelId) {
        //查询通道和通道的所属设备 状态是否处于在线状态
        DeviceChannel deviceChannel = this.deviceChannelService.getOne(Wrappers.<DeviceChannel>lambdaQuery().eq(DeviceChannel::getChannelId, channelId));
        if (Objects.isNull(deviceChannel) || !deviceChannel.getStatus()) {
            return Result.failed("播放放失败，设备不在线");
        }
        Device device = this.getOne(Wrappers.<Device>lambdaQuery().eq(Device::getDeviceId, deviceChannel.getDeviceId()));
        if (Objects.isNull(device) || !device.getOnline()) {
            return Result.failed("播放放失败，设备不在线");
        }
        String streamId = sipCommander.playStream(device, channelId);
        if (log.isDebugEnabled()){
            log.debug("通道号:{},播放视频,streamId为：{}",channelId,streamId);
        }
        if (StrUtil.isEmpty(streamId)) {
            return Result.failed("播放失败");
        }
        StreamInfo mediaServerServiceMedia = this.zlkMediaServerService.getMedia(zlkMediaServerInfo, SipConstant.APP_ID, streamId);
        return Result.success("播放成功", mediaServerServiceMedia);
    }

    @Override
    public Result<Object> playStopStream(String streamId, String channelId) {
    /*    //查询通道和通道的所属设备 状态是否处于在线状态
        DeviceChannel deviceChannel = this.deviceChannelService.getOne(Wrappers.<DeviceChannel>lambdaQuery().eq(DeviceChannel::getChannelId, channelId));
        if (Objects.isNull(deviceChannel) || !deviceChannel.getStatus()) {
            return Result.failed("停止失败,设备不在线");
        }
        Device device = this.getOne(Wrappers.<Device>lambdaQuery().eq(Device::getDeviceId, deviceChannel.getDeviceId()));
        if (Objects.isNull(device) || !device.getOnline()) {
            return Result.failed("停止失败，设备不在线");
        }
        boolean b = sipCommander.streamByeCmd(streamId, channelId);
        if (b) {
            return Result.success("停止播放成功", null);
        }
        return Result.failed("停止播放失败");*/
        return Result.success();
    }
}
